package com.buyouchina.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtils {

	/**
	 * 取得昨天的时间区间
	 * */
	public static Date[] getPreviousDayInterval() {
		Date[] dates = new Date[2];
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			c.add(Calendar.DATE, -1);
			String startTime = sdf.format(c.getTime()) + " 00:00:00";
			String endTime = sdf.format(c.getTime()) + " 23:59:59";
			dates[0] = sdf1.parse(startTime);
			dates[1] = sdf1.parse(endTime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dates;
	}
	
	/**
	 * 取得今天的时间区间
	 * */
	public static Date[] getTodayInterval() {
		Date[] dates = new Date[2];
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			String startTime = sdf.format(new Date()) + " 00:00:00";
			String endTime = sdf.format(new Date()) + " 23:59:59";
			dates[0] = sdf1.parse(startTime);
			dates[1] = sdf1.parse(endTime);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dates;
	}

	/**
	 * 取得昨天是几号
	 * */
	public static Date getPreviousDay() {
		Date date = new Date();
		Calendar c = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			c.add(Calendar.DATE, -1);
			date = sdf.parse(sdf.format(c.getTime()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 取得上周的时间区间
	 * */
	public static Date[] getPreviousWeekInterval() {
		Date[] dates = new Date[2];
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, -7);
		c.set(Calendar.DAY_OF_WEEK, 1);
		dates[0] = c.getTime();
		c.roll(Calendar.DAY_OF_WEEK, -1);
		dates[1] = c.getTime();
		return dates;
	}

	/**
	 * 取得本周的时间区间
	 * */
	public static Date[] getCurrentWeekInterval() {
		Date[] dates = new Date[2];
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, 1);
		c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		dates[0] = c.getTime();
		c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
		dates[1] = c.getTime();
		return dates;
	}

	/**
	 * 取得上个月的时间区间
	 * */
	public static Date[] getPreviousMonthInterval() {
		Date[] date = new Date[2];
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		c.set(Calendar.DATE, 1);
		date[0] = c.getTime();
		c.roll(Calendar.DATE, -1);
		date[1] = c.getTime();
		return date;
	}

	/**
	 * 取得本月的时间区间
	 * */
	public static Date[] getCurrentMonthInterval() {
		Date[] date = new Date[2];
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DATE, 1);
		date[0] = c.getTime();
		c.roll(Calendar.DATE, -1);
		date[1] = c.getTime();
		return date;
	}

	/**
	 * 取得去年的时间区间
	 * */
	public static Date[] getPreviousYearInterval() {
		Date[] date = new Date[2];
		Calendar c = Calendar.getInstance();
		c.add(Calendar.YEAR, -1);
		c.set(Calendar.DAY_OF_YEAR, 1);
		date[0] = c.getTime();
		c.roll(Calendar.DAY_OF_YEAR, -1);
		date[1] = c.getTime();
		return date;
	}

}
